package com.gpoint.rdp.door.snTimeSeq.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.gpoint.rdp.base.core.util.string.StringUtil;
import com.gpoint.rdp.base.core.entity.ResultMessage;
import com.gpoint.rdp.base.framework.page.PageList;
import com.gpoint.rdp.base.framework.page.PageResult;
import com.alibaba.fastjson.JSONObject;
import com.gpoint.rdp.api.base.query.QueryFilter;
import com.gpoint.rdp.base.web.controller.GenericController;
import com.gpoint.rdp.base.web.json.PageJson;
import com.gpoint.rdp.base.web.util.RequestUtil;
import com.gpoint.rdp.door.snTimeSeq.service.SnTimeSeqService;
import com.gpoint.rdp.door.snTimeSeq.service.SnTimeSeqQueryService;
import com.gpoint.rdp.door.snTimeSeq.persistence.dao.SnTimeSeqQueryDao;
import com.gpoint.rdp.door.snTimeSeq.persistence.entity.SnTimeSeqPo;
import com.gpoint.rdp.base.core.util.JacksonUtil;

import java.util.List;

// TODO: Auto-generated Javadoc
/**
 * 设备SN和时间段关联表 控制类
 *
 *<pre> 
 * 开发公司：苏州东大金点信息技术有限公司
 * 开发人员：唐红锋
 * 邮箱地址：55872655@qq.com
 * 创建时间：2019-11-26 13:53:24
 *</pre>
 */
@Controller
@RequestMapping("/rdp/snTimeSeq/snTimeSeq/")
public class SnTimeSeqController extends GenericController{
	
	/** The sn time seq service. */
	@Resource
	private SnTimeSeqService snTimeSeqService;
	
	/** The sn time seq query service. */
	@Resource
	private SnTimeSeqQueryService snTimeSeqQueryService;
	
	

	/**
	 * List.  list sn and device jsp 
	 *
	 * @param request the request
	 * @param response the response
	 * @return the model and view
	 * @throws Exception the exception
	 */
	@RequestMapping("list")
	 public ModelAndView list(HttpServletRequest request,HttpServletResponse response) throws Exception{
	  ModelAndView mv=getAutoView();
	  return mv;
	  
	 }
	
	
	/**
	 * 【设备SN和时间段关联表】列表(分页条件查询)数据.
	 *
	 * @param request the request
	 * @param reponse the reponse
	 * @return the page json
	 * @throws Exception the exception
	 */
	@RequestMapping("listJson")
	public @ResponseBody PageJson listJson(HttpServletRequest request,HttpServletResponse reponse) throws Exception{
		QueryFilter queryFilter = getQuerFilter(request);
		try{

			String listData = this.snTimeSeqQueryService.querypage(queryFilter);
			PageList<SnTimeSeqPo> snTimeSeqList = null;
			if(JacksonUtil.isJsonObject(listData)){
				List<SnTimeSeqPo> list = SnTimeSeqPo.fromJsonArrayString(JacksonUtil.getString(listData, "data"));
				PageResult pageResult = PageResult.fromJson(JacksonUtil.getString(listData, "pageResult"));
				snTimeSeqList = new PageList<SnTimeSeqPo>(list, pageResult);
			}
			
			return new PageJson(snTimeSeqList);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new PageJson();
		}
	}
	
	/**
	 * 编辑【设备SN和时间段关联表】信息页面.
	 *
	 * @param request the request
	 * @param response the response
	 * @return the model and view
	 * @throws Exception the exception
	 */
	@RequestMapping("edit")
	public ModelAndView edit(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String preUrl= RequestUtil.getPrePage(request);
		String id=RequestUtil.getString(request, "id");
		SnTimeSeqPo snTimeSeq=null;
		if(StringUtil.isNotEmpty(id)){
			String data = null;
			data = snTimeSeqQueryService.get(id);
			snTimeSeq = SnTimeSeqPo.fromJsonString(data);
		}

		return getAutoView().addObject("snTimeSeq", snTimeSeq).addObject("returnUrl", preUrl);
	}
	
	/**
	 * 编辑【设备SN和时间段关联表】信息页面.
	 *
	 * @param request the request
	 * @param response the response
	 * @return the model and view
	 * @throws Exception the exception
	 */
	@RequestMapping("flowEdit")
	public ModelAndView flowEdit(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String preUrl= RequestUtil.getPrePage(request);
		String id=RequestUtil.getString(request, "id");
		SnTimeSeqPo snTimeSeq=null;
		if(StringUtil.isNotEmpty(id)){
			String data = null;
			data = snTimeSeqQueryService.get(id);
			snTimeSeq = SnTimeSeqPo.fromJsonString(data);
		}
		return getAutoView().addObject("snTimeSeq", snTimeSeq).addObject("returnUrl", preUrl);
	}
	
	/**
	 * 【设备SN和时间段关联表】明细页面.
	 *
	 * @param request the request
	 * @param response the response
	 * @return the model and view
	 * @throws Exception the exception
	 */
	@RequestMapping("get")
	public ModelAndView get(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String preUrl= RequestUtil.getPrePage(request);
		String id=RequestUtil.getString(request, "id");
		SnTimeSeqPo snTimeSeq=null;
		if(StringUtil.isNotEmpty(id)){
			String data = null;
			data = snTimeSeqQueryService.get(id);
			snTimeSeq = SnTimeSeqPo.fromJsonString(data);
		}
		return getAutoView().addObject("snTimeSeq", snTimeSeq).addObject("returnUrl", preUrl);
	}
	
	/**
	 *  
	 * 保存【设备SN和时间段关联表】信息.
	 *
	 * @param request the request
	 * @param response the response
	 * @throws Exception the exception
	 */
	@RequestMapping("save")
	public void save(HttpServletRequest request,HttpServletResponse response) throws Exception{
		ResultMessage message=null;
		try {
			String sn = RequestUtil.getString(request, "sn");
			String timeseg = RequestUtil.getString(request, "timeseg");
			
			SnTimeSeqPo po=new SnTimeSeqPo();
			 po.setSn(sn);
			 po.setTimeseg(timeseg);
			//默认是0
			 po.setStatus("0"); 
			
			 String json=JSONObject.toJSONString(po);
			 
			 
			snTimeSeqService.save(json);
			message=new ResultMessage(ResultMessage.SUCCESS, "保存设备SN和时间段关联表成功");
		} catch (Exception e) {
			message=new ResultMessage(ResultMessage.FAIL, "对设备SN和时间段关联表操作失败,"+e.getMessage());
			logger.error("对设备SN和时间段关联表操作失败，" + e.getMessage(),e);
		}
		writeResultMessage(response.getWriter(), message);
	}
	
	/**
	 *  批量删除【设备SN和时间段关联表】记录.
	 *
	 * @param request the request
	 * @param response the response
	 * @throws Exception the exception
	 */
	@RequestMapping("remove")
	public void remove(HttpServletRequest request,HttpServletResponse response) throws Exception{
		ResultMessage message=null;
		try {
			//获得待删除的id
			String[] ids=RequestUtil.getStringAryByStr(request, "id");
			snTimeSeqService.deleteByIds(ids);
			message=new ResultMessage(ResultMessage.SUCCESS, "删除设备SN和时间段关联表成功");
		} catch (Exception e) {
			message=new ResultMessage(ResultMessage.FAIL, "删除设备SN和时间段关联表失败，" + e.getMessage());
			logger.error("删除设备SN和时间段关联表失败，" + e.getMessage(),e);
		}
		writeResultMessage(response.getWriter(), message);
	}
	

}